//
//  StoreSearchViewController.m
//  much
//
//  Created by weijunjie on 2019/1/4.
//  Copyright © 2019年 iOS. All rights reserved.
//

#import "StoreSearchViewController.h"
#import "StoreSearchToolView.h"
#import "MallHomeContentDataModel.h"
#import "MallHomeTwoGoodsCollectionCell.h"
#import "StoreDetailViewController.h"
#import "SearchFilterModel.h"
#import "GoodsModel.h"

@interface StoreSearchViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIView *searchView;
@property (nonatomic, strong) StoreSearchToolView *searchToolView;
@property (nonatomic, strong) NullStateView * nullStateView;//网络错误的提示
@end

@implementation StoreSearchViewController

- (void)_reLayout {
    [self setupNavigationItems];
    [self searchToolView];
}

- (void)viewControllerBack {
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)setupNavigationItems {
    __weakConfigure
    [self.navigationView addCustomView:self.searchView makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(__weakSelf.navigationView).offset(-15);
        make.left.equalTo(__weakSelf.navigationView).offset(40);
        make.bottom.equalTo(__weakSelf.navigationView).offset(-7);
        make.height.mas_equalTo(30);
    } callback:^(UIView *view) {
        [__weakSelf.navigationController popViewControllerAnimated:NO];
    }];
}

- (void)setData:(NSArray *)data {
    _data = data;
    [self.collectionView reloadData];
    __weakConfigure
    if (self.filterModel.pageIndex == 0 && self.collectionView.mj_footer == nil) {
        [self.nullStateView removeFromSuperview];
        if (data.count == 0) {
            [self.view addSubview:self.nullStateView];
            [self.nullStateView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.equalTo(self.view);
                make.top.equalTo(self.searchToolView.mas_bottom);
            }];
        }
        
        self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [__weakSelf requestSearchInfo];
        }];
        [self.collectionView setContentOffset:CGPointZero];
        self.filterModel.pageIndex++;
    }
}

- (void)setFilterModel:(SearchFilterModel *)filterModel {
    _filterModel = filterModel;
    self.searchToolView.filterModel = filterModel;
}

#pragma mark - 懒加载
- (UIView *)searchView {
    if (!_searchView) {
        _searchView = [[UIView alloc] init];
        _searchView.backgroundColor = Color244;
        _searchView.layer.cornerRadius = 15;
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = APPFONT(14);
        titleLabel.textColor = Color34;
        titleLabel.text = self.filterModel.keyWord;
        [_searchView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self.searchView);
            make.left.equalTo(self.searchView).offset(15);
        }];
    }
    return _searchView;
}

- (StoreSearchToolView *)searchToolView {
    if (!_searchToolView) {
        _searchToolView = [[StoreSearchToolView alloc] init];
        _searchToolView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_searchToolView];
        [_searchToolView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(38);
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.navigationView.mas_bottom);
        }];
        __weakConfigure
        _searchToolView.clickToolBlock = ^{
            __weakSelf.collectionView.mj_footer = nil;
            [__weakSelf requestSearchInfo];
        };
    }
    return _searchToolView;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat width = (ScreenWidth - 35) * 0.5; // 图片高度
        layout.itemSize = CGSizeMake(width, [MallHomeContentDataModel twoGoodsPerLineHeight]);
        layout.minimumLineSpacing = 5;
        layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(15);
            make.right.equalTo(self.view).offset(-15);
            make.top.equalTo(self.searchToolView.mas_bottom);
            make.bottom.equalTo(self.view);
        }];
        [_collectionView registerClass:[MallHomeTwoGoodsCollectionCell class] forCellWithReuseIdentifier:[MallHomeTwoGoodsCollectionCell cellClass]];
        __weakConfigure
        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __weakSelf.filterModel.pageIndex = 0;
            __weakSelf.collectionView.mj_footer = nil;
            [__weakSelf requestSearchInfo];
        }];
    }
    return _collectionView;
}

- (NullStateView *)nullStateView {
    if (!_nullStateView) {
        _nullStateView = [[NullStateView alloc] initWithFrame:self.view.frame fromViewType:NoSearchResultsType];
    }
    return _nullStateView;
}

#pragma mark - <UICollectionViewDataSource,UICollectionViewDelegate>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.data.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MallHomeTwoGoodsCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[MallHomeTwoGoodsCollectionCell cellClass] forIndexPath:indexPath];
    GoodsModel *model = self.data[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    StoreDetailViewController *vc = [[StoreDetailViewController alloc] init];
    GoodsModel *model = self.data[indexPath.row];
    vc.spuSid = model.sid;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)requestSearchInfo {
    self.beRequest = YES;
    if (self.filterModel.pageIndex == 0) {
        [SVProgressHUD showWithStatus:@"加载中..."];
    }
    [MuchStoreRequestManager storeSearchWithFilterModel:self.filterModel successBlock:^(NSArray *responseObject, NSInteger totalPages) {
        
        if (self.filterModel.pageIndex == 0) {
            [self.collectionView.mj_header endRefreshing];
            self.data = responseObject;
        } else {
            [self.collectionView.mj_footer endRefreshing];
            NSMutableArray *arrM = [NSMutableArray arrayWithArray:self.data];
            [arrM addObjectsFromArray:responseObject];
            self.data = arrM;
            if (self.filterModel.pageIndex >= totalPages - 1) {
                self.collectionView.mj_footer.state = MJRefreshStateNoMoreData;
            } else {
                self.collectionView.mj_footer.state = MJRefreshStateIdle;
            }
            self.filterModel.pageIndex++;
        }
        
        self.beRequest = NO;
        [SVProgressHUD dismiss];
        [self.collectionView reloadData];
    } failureBlock:^(NSString *error) {
        self.beRequest = NO;
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
        [SVProgressHUD showErrorWithStatus:error];
    }];
}

@end
